package com.example.smartcity_ikun3.ui.home.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity_ikun3.R
import com.example.smartcity_ikun3.logic.model.WeatherModel
import com.example.smartcity_ikun3.logic.network.RetrofitService
import com.example.smartcity_ikun3.logic.network.SmartApi
import com.example.smartcity_ikun3.logic.util.Util.show
import com.example.smartcity_ikun3.ui.home.HomeViewModel
import com.example.smartcity_ikun3.ui.home.adapter.ReAdapterWeather
import com.example.smartcity_ikun3.ui.home.model.WeatherEntity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class WeatherActivity : AppCompatActivity() {

    private val viewModel by lazy {
        ViewModelProviders.of(this).get(HomeViewModel::class.java)
    }

    private lateinit var mWeatherRecyclerView: RecyclerView
    private lateinit var mWeatherAdd: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_weather)
        title = "天气预报"
        mWeatherRecyclerView = findViewById(R.id.WeatherAddRecyclerView)
        mWeatherAdd = findViewById(R.id.WeatherThisRecyclerView)


        // 今日天气
        setThis()
        // 7天气
        setWeatherAdd()

    }

    private fun setWeatherAdd() {
        val array = ArrayList<WeatherEntity>()

        RetrofitService.create<SmartApi>().getWeather().enqueue(object : Callback<WeatherModel> {
            override fun onFailure(p0: Call<WeatherModel>, p1: Throwable) {
                "加载天气失败！".show()
                Log.d("NET", "onResponse: Weather ---> ${p1.message}")
            }

            override fun onResponse(p0: Call<WeatherModel>, p1: Response<WeatherModel>) {
                val body = p1.body()
                if (body != null) if (body.code == 200){
                    for (i in 1..7){
                        val wendu = "${body.data.maxTemperature}' / ${body.data.minTemperature}'"
                        array.add(
                            WeatherEntity(
                                R.mipmap.weather1,
                                body.data.weather,
                                wendu,
                                "空气质量: ${body.data.air}",
                                "湿度: ${body.data.humidity}"
                            )
                        )

                        // array.add(WeatherEntity(R.mipmap.weather1, "1","2", "3","4"))
                        mWeatherAdd.apply {
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter = ReAdapterWeather(array)
                        }
                    }

                }else "获取天气失败！".show()
            }
        })
    }







    private fun setThis() {
        val array = ArrayList<WeatherEntity>()

        RetrofitService.create<SmartApi>().getWeather().enqueue(object : Callback<WeatherModel> {
            override fun onFailure(p0: Call<WeatherModel>, p1: Throwable) {
                "加载天气失败！".show()
                Log.d("NET", "onResponse: Weather ---> ${p1.message}")
            }

            override fun onResponse(p0: Call<WeatherModel>, p1: Response<WeatherModel>) {
                val body = p1.body()
                if (body != null) if (body.code == 200){
                    val wendu = "${body.data.maxTemperature}' / ${body.data.minTemperature}'"
                    array.add(
                        WeatherEntity(
                        R.mipmap.weather1,
                        body.data.weather,
                        wendu,
                        "空气质量: ${body.data.air}",
                        "湿度: ${body.data.humidity}"
                    )
                    )

                    // array.add(WeatherEntity(R.mipmap.weather1, "1","2", "3","4"))
                    mWeatherRecyclerView.apply {
                        this.layoutManager = LinearLayoutManager(context)
                        this.adapter = ReAdapterWeather(array)
                    }
                }else "获取天气失败！".show()
            }
        })
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}